﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RCREEE.Library;
using RCREEE.Library.Entities;
using System.Data;
using System.Drawing;

namespace RCREEE.Web
{
    public partial class EnergyConsumptionDevelopment_ViewAll : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                //// Add the language code to session
                //Session.Add("LanguageCode", 1033);
                int countryId = 0;

                if (Request["C"] != null && Request["C"].ToString() != "")
                    countryId = Convert.ToInt32(Request["C"]);

                else if (Session["CountryId"] != null)
                    countryId = Convert.ToInt32(Session["CountryId"]);

                if (countryId != 0)
                {
                    ddlCountries.DataBind();
                    ddlCountries.SelectedValue = Convert.ToString(countryId);
                    PopulateGrid();
                }
            }
        }

        protected void ddlCountries_SelectedIndexChanged(object sender, EventArgs e)
        {
            PopulateGrid();
        }

        private void PopulateGrid()
        {
            if (ddlCountries.SelectedValue == null || ddlCountries.SelectedValue == "0")
            {
                gvEnergyItems.DataSource = null;
                gvEnergyItems.DataBind();
            }
            else
            {
                EnergyConsumptionDevelopmentItemValue _energy = new EnergyConsumptionDevelopmentItemValue();
                DataTable dtEnergyItems = _energy.GetAllEnergyConsumptionDevelopmentItemValues(Convert.ToInt32(Session["LanguageCode"]), Convert.ToInt32(ddlCountries.SelectedValue));
                DataTable dtFinalTable = dtEnergyItems.Clone();


                DataRow[] drParents = dtEnergyItems.Select("ParentItemId = 0");

                foreach (DataRow dr in drParents)
                {
                    int level = 1;
                    dtFinalTable.Rows.Add(dr.ItemArray);
                    AddChildren(dtEnergyItems, ref dtFinalTable, Convert.ToInt32(dr["ItemId"]), ref level);
                }

                dtFinalTable.Columns.Remove("ItemId");
                dtFinalTable.Columns.Remove("ParentItemId");
                //dtFinalTable.Columns.Remove("IsMajor");

                gvEnergyItems.DataSource = dtFinalTable;
                gvEnergyItems.DataBind();
            }
        }

        private void AddChildren(DataTable dtSource, ref DataTable dtDestination, int ItemId, ref int level)
        {
            int currentLevel = level;
            level++;
            DataRow[] drChildren = dtSource.Select("ParentItemId=" + ItemId.ToString());
            if (drChildren.Length > 0)
            {
                foreach (DataRow drChild in drChildren)
                {
                    drChild["ItemName"] = GetSpacing(currentLevel) + drChild["ItemName"].ToString();
                    drChild.AcceptChanges();
                    dtDestination.Rows.Add(drChild.ItemArray);

                    AddChildren(dtSource, ref dtDestination, Convert.ToInt32(drChild["ItemId"]), ref level);
                }
            }
        }

        private string GetSpacing(int level)
        {
            string spacing = "";
            for (int i = 0; i < level; i++)
                spacing += " -- ";

            return spacing;
        }

        protected void gvEnergyItems_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                foreach (TableCell _cell in e.Row.Cells)
                    _cell.Width = Unit.Pixel(100);
            }
            else
            {
                e.Row.Cells[0].Width = Unit.Pixel(400);
                e.Row.Cells[0].HorizontalAlign = HorizontalAlign.Left;

            }
            // hide is major column
            e.Row.Cells[1].Visible = false;
        }

        protected void gvEnergyItems_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowIndex > -1)
                if (((CheckBox)e.Row.Cells[1].Controls[0]).Checked)
                //{
                //    for (int i = 1; i < gvEnergyItems.Columns.Count; i++)
                //    {
                //        e.Row.Cells.RemoveAt(i);
                //    }
                //    e.Row.Cells[0].ColumnSpan = ((DataTable)gvEnergyItems.DataSource).Columns.Count;
                //    e.Row.Cells[0].HorizontalAlign = HorizontalAlign.Center;
                //    e.Row.Cells[0].BackColor = Color.FromArgb(2, 104, 176);
                //    e.Row.Cells[0].ForeColor = System.Drawing.Color.White;
                //}
                {
                    int colSpan = ((DataTable)gvEnergyItems.DataSource).Columns.Count;
                    for (int i = 1; i < colSpan; i++)
                    {
                        e.Row.Cells[i].Visible = false;
                        e.Row.Cells[i].Controls.Clear();
                    }

                    e.Row.Cells[0].Attributes["ColSpan"] = (colSpan).ToString();
                    e.Row.Cells[0].HorizontalAlign = HorizontalAlign.Center;
                    e.Row.Cells[0].BackColor = Color.FromArgb(2, 104, 176);
                    e.Row.Cells[0].ForeColor = System.Drawing.Color.White;
                }



            //if (e.Row.RowType == DataControlRowType.DataRow)
            //{
            //    if (e.Row.RowIndex == 0)
            //        e.Row.Cells[0].RowSpan = ((DataTable)gvEnergyItems.DataSource).Rows.Count;
            //    else
            //        e.Row.Cells.Remove(e.Row.Cells[0]);
            //}

        }
    }
}